package com.yanqu.road.server.manager.activity.musicroomrank;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossMusicRoomRankActivityMgr extends TempCrossMgr {
    private static Map<Integer, MusicRoomRankActivity> activityMap = new ConcurrentHashMap<>();
    @Override
    public boolean removeExpireData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        reloadActivity();
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        for (MusicRoomRankActivity activity : activityMap.values()) {
            activity.save();
        }
        return true;
    }

    public static synchronized void reloadActivity() {
        getLogger().info("reload CrossMusicRoomRankActivityMgr start");
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.MusicRoomRank.getValue());
        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        for (ActivityInfo item : activityInfoMap.values()) {
            reloadActivity(item);
        }
        getLogger().info("reload CrossMusicRoomRankActivityMgr end");
    }

    public static void reloadActivity(ActivityInfo activityInfo) {
        if (activityInfo == null) {
            return;
        }
        int activityId = activityInfo.getActivityId();
        if (CrossMusicRoomRankConfigMgr.getConfig(activityId) == null) {
            CrossMusicRoomRankConfigMgr.reloadConfig(activityInfo);
        }

        MusicRoomRankActivity tempActivity = new MusicRoomRankActivity(activityInfo);
        activityMap.put(activityId, tempActivity);
    }

    public static MusicRoomRankActivity getActivity(int activityId) {
        MusicRoomRankActivity activity = activityMap.get(activityId);
        if (activity == null) {
            synchronized (activityMap) {
                activity = activityMap.get(activityId);
                if (activity == null) {
                    ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
                    if (activityInfo == null) {
                        return null;
                    }
                    if (CrossMusicRoomRankConfigMgr.getConfig(activityId) == null) {
                        CrossMusicRoomRankConfigMgr.reloadConfig(activityInfo);
                    }

                    activity = new MusicRoomRankActivity(activityInfo);
                    activityMap.put(activityId, activity);
                }
            }
        }
        return activity;
    }

    public static void activityTask() {
        for (MusicRoomRankActivity activity : activityMap.values()) {
            try {
                activity.settleTask();
            } catch (Exception e) {
                getLogger().error("activityTask error", e);
            }
        }
    }
}
